﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;

namespace Cofe.Web.Utils
{
    public static class HttpResponseMessageExtensions
    {
        public static HttpResponseMessage SetLastModified(this HttpResponseMessage response, DateTime? lastModifiedUtc)
        {
            if (lastModifiedUtc.HasValue)
                response.Content.Headers.LastModified = lastModifiedUtc.Value.ToLocalTime();

            return response;
        }
        public static HttpResponseMessage SetMaxAge(this HttpResponseMessage response, TimeSpan? maxAge)
        {
            if (maxAge.HasValue)
                response.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    Public = true,
                    MaxAge = maxAge,
                    MustRevalidate = false 
                };

            return response;

        }

    
        public static HttpResponseMessage SetMediaType(this HttpResponseMessage response, WebResourceAttribute resourceAttribute)
        {
            if (resourceAttribute != null && resourceAttribute.MediaType != MediaType.None &&
                resourceAttribute.MediaType != MediaType.Unknown)
                //response.Headers.Add("Content-Type", "application/xml; charset=utf-8");
                response.Content.Headers.ContentType = new MediaTypeHeaderValue(
                    DecoratorExtension.GetMIMEType(resourceAttribute.MediaType));               

            return response;

        }

    }
}
